package com.ruitu.health.base;

import android.text.TextUtils;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.huawei.hmf.tasks.OnCompleteListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.push.HmsMessaging;
import com.ruitu.arad.AradApplication;
import com.ruitu.arad.AradApplicationConfig;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.support.util.MobileUtil;

import me.jessyan.progressmanager.ProgressManager;
import okhttp3.OkHttpClient;

public class MyApp extends AradApplication {
    @Override
    protected AradApplicationConfig appConfig() {
        return new AradApplicationConfig();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        this.mOkHttpClient = ProgressManager.getInstance().with(new OkHttpClient.Builder()).build();
        Fresco.initialize(this);

        if (MobileUtil.isEMUI()) {// 获取华为推送token
            getToken();
//            turnOnPush();
        } else {
            Logg.i("非Emui系统..");
        }
    }

    //这里我就不写管理类了,捡个懒,直接在 Application 中管理单例 Okhttp
    private static OkHttpClient mOkHttpClient;

    public static OkHttpClient getOkHttpClient() {
        return mOkHttpClient;
    }

    public void getToken() {
        new Thread() {
            @Override
            public void run() {
                try {
                    String getToken = HmsInstanceId.getInstance(MyApp.this).getToken("101238643", "HCM");
                    if (!TextUtils.isEmpty(getToken)) {
                        Logg.i("华为推送token = " + getToken);
                    } else {
                        Logg.i("华为推送token = " + getToken);
                    }
                } catch (Exception e) {
                    Logg.i("getToken failed..." + e.getMessage());
                }
            }
        }.start();
    }

    public void turnOnPush() {// 这个方法一般情况下无需调用
        HmsMessaging.getInstance(this).turnOnPush().addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(Task<Void> task) {
                if (task.isSuccessful()) {
                    Logg.i("turnOnPush Complete");
                } else {
                    Logg.i("turnOnPush failed: ret=" + task.getException().getMessage());
                }
            }
        });
    }
}
